package br.com.lambdacore.domain.assignable;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import br.com.lambdacore.collection.CollectionsUtil;
import br.com.lambdacore.collection.MapUtil;

public class Drawer {

	@SuppressWarnings("rawtypes")
	public void draw(Collection<? extends Assignable> assignables) {
		List<? extends Assignable> assignablesTo = CollectionsUtil
				.arrayList(assignables);
		Collections.shuffle(assignablesTo);
		List<? extends Assignable> assignablesFrom = CollectionsUtil
				.arrayList(assignablesTo);
		Collections.rotate(assignablesFrom, 1);
		Map<Assignable, Assignable> map = MapUtil.hashMap();
		MapUtil.fillMap(map, assignablesTo, assignablesFrom);
		this.relate(map, assignablesTo, assignablesFrom);
	}

	@SuppressWarnings({
			"rawtypes", "unchecked"
	})
	private void relate(Map<Assignable, Assignable> map,
			List<? extends Assignable> assignablesTo,
			List<? extends Assignable> assignablesFrom) {
		Set<Entry<Assignable, Assignable>> entrySet = map.entrySet();
		for (Entry<Assignable, Assignable> entry : entrySet) {
			entry.getKey().setRelated(entry.getValue());
		}
	}
}
